﻿Module UI

  Public BGColorOdd As Color = Color.LightGray
  Public BGColorEven As Color = Color.LightCoral


  Public Sub InitPB(ByVal PB As ToolStripProgressBar, ByVal MaxValue As Long)

    Dim ts As ToolStrip = PB.GetCurrentParent()
    Dim sumWidth As Integer

    For i = 0 To ts.Items.Count - 1

      If PB.Name <> ts.Items(i).Name Then
        sumWidth = ts.Items(i).Width
      End If

    Next

    PB.Width = ts.Width - sumWidth - 50

    PB.Maximum = MaxValue
    PB.Value = 0
    PB.Visible = True

  End Sub


  Public Sub DGV_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)

    If e.RowIndex > -1 Then

      If e.RowIndex Mod 2 = 0 Then
        e.CellStyle.BackColor = Color.LavenderBlush
      Else
        e.CellStyle.BackColor = Color.FromArgb(250, 250, 250)
      End If

    End If

  End Sub


  ''' <summary>
  ''' Проверка открыта ли форма
  ''' </summary>
  ''' <param name="frm">Форма которую нужно открыть</param>
  ''' <param name="mainfrm">Форма родитель (Main)</param>
  ''' <param name="modal">Параметр модальности 0 - не модальное, 1 - модальное</param>
  Public Sub OpenForm(ByVal frm As Form, Optional ByVal mainfrm As Form = Nothing, Optional ByVal modal As Byte = 0)

    For Each f As Form In Application.OpenForms
      If f.Name = frm.Name Then
        Dim WinState As System.Windows.Forms.FormWindowState = f.WindowState
        f.WindowState = FormWindowState.Normal
        f.Activate()
        If WinState <> FormWindowState.Minimized Then
          f.WindowState = WinState
        End If
        Return
      End If
    Next

    If mainfrm IsNot Nothing Then
      frm.MdiParent = mainfrm
    Else
      frm.StartPosition = FormStartPosition.CenterScreen
    End If

    If modal = 1 And mainfrm Is Nothing Then
      frm.ShowDialog()
    Else
      frm.Show()
    End If


    frm.WindowState = FormWindowState.Maximized

  End Sub


  ''' <summary>
  ''' События для клавиш в элементе DataGridView
  ''' </summary>
  Public Sub DGVKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)

    If DirectCast(sender, DataGridView).Rows.GetRowCount(DataGridViewElementStates.Visible) = 0 Then Exit Sub

    If e.KeyCode = Keys.Home Then
      DirectCast(sender, DataGridView).Rows(0).Selected = True
      DirectCast(sender, DataGridView).Rows(0).Cells(0).Selected = True
    End If

    If e.KeyCode = Keys.End Then
      DirectCast(sender, DataGridView).Rows(DirectCast(sender, DataGridView).Rows.GetLastRow(DataGridViewElementStates.Visible)).Selected = True
      DirectCast(sender, DataGridView).Rows(DirectCast(sender, DataGridView).Rows.GetLastRow(DataGridViewElementStates.Visible)).Cells(0).Selected = True
    End If

    If e.KeyCode = Keys.Space Then
      If DirectCast(sender, DataGridView).SelectedRows.Count > 0 Then
        DGVCheck(sender, New DataGridViewCellEventArgs(0, DirectCast(sender, DataGridView).SelectedRows(0).Index))
      End If
    End If

  End Sub


  ''' <summary>
  ''' Изменения статуса Чекбокса
  ''' </summary>
  Private Sub DGVCheck(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)

    If e.RowIndex = -1 Then
      Return
    End If

    If DirectCast(sender, DataGridView)(e.ColumnIndex, e.RowIndex).[GetType]() = New DataGridViewCheckBoxCell().[GetType]() Then
      Dim selcheck As DataGridViewCheckBoxCell = DirectCast(DirectCast(sender, DataGridView)(e.ColumnIndex, e.RowIndex), DataGridViewCheckBoxCell)
      If selcheck.Value Is Nothing Then
        selcheck.Value = 1
      Else
        selcheck.Value = Nothing
      End If
    End If

  End Sub


  ''' <summary>
  ''' Показывает диалог с сообщением об ошибке
  ''' </summary>
  ''' <param name="msg">Текст ошибки</param>
  ''' <param name="ex">Исключение, если нет - Nothing</param>
  Public Sub MsgErr(ByVal msg As String, Optional ByVal ex As Exception = Nothing)

    If ex Is Nothing = False Then
      If msg <> "" Then msg += vbNewLine
      msg += ex.Message
    End If

    MessageBox.Show(msg, "Ошибка выполнения", MessageBoxButtons.OK, MessageBoxIcon.Error)

  End Sub



  ''' <summary>
  ''' Событие при активации поля поиска
  ''' </summary>
  Public Sub StrFind_Enter(ByVal sender As Object, ByVal e As EventArgs)
    If DirectCast(sender, TextBox).Text = Params.StrSearchLabel Then
      DirectCast(sender, TextBox).Text = ""
      DirectCast(sender, TextBox).ForeColor = Color.Black
    End If
  End Sub



  ''' <summary>
  ''' Событие при деактивации поля поиска
  ''' </summary>
  Public Sub StrFind_Leave(ByVal sender As Object, ByVal e As EventArgs)
    If DirectCast(sender, TextBox).Text = "" Then
      DirectCast(sender, TextBox).Text = Params.StrSearchLabel
      DirectCast(sender, TextBox).ForeColor = Color.DarkGray
    End If
  End Sub


End Module
